﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Net.Mail;
using System.Text;
namespace DentistOffice
{
    public partial class NoweHaslo : System.Web.UI.Page
    {
        private static Random _random = new Random();

        protected void Page_Load(object sender, EventArgs e)
        {
        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            using (DentistEntities dB = new DentistEntities())
            {
                var query = dB.Patients.First(qr => qr.login == loginTextBox1.Text);
                if (query == null)
                {
                    infoPlaceHolder.Visible = true;
                    infoLabel.Text = "zły login";
                    return;
                }
                if (query.email != emailTextBox2.Text)
                {
                    infoPlaceHolder.Visible = true;
                    infoLabel.Text = "zły email";
                    return;
                }
                sendMail();
            }
        }

        void sendMail()
        {
            try
            {
                bool auth = true;

                string newPass = getNewPassword();
                string message = getMessage(newPass);

                MailAddress fromEmail = new MailAddress("twatral@studentlive.pl");
                MailMessage email = new MailMessage();
                email.From = fromEmail;
                //email.To.Add(mailTextBox2.Text);

                /*string newPass = getNewPassword();
                string message = getMessage(newPass);*/
                email.To.Add(emailTextBox2.Text);
                email.Subject = "Nowe hasło";
                email.IsBodyHtml = true;
                email.Body = message;

                SmtpClient client = new SmtpClient("smtp.live.com", 587);
                client.EnableSsl = true;
                if (auth)
                {
                    System.Net.NetworkCredential SMTPUserInfo = new System.Net.NetworkCredential("twatral@studentlive.pl", "glory99");
                    client.UseDefaultCredentials = false;
                    client.Credentials = SMTPUserInfo;
                }
                client.Send(email);
            }
            catch(Exception ex)
            {
                loginLabel1.Text = ex.Message;
            }
        }
        //utworzenie nowego hasła
        protected string getNewPassword()
        {
            StringBuilder builder = new StringBuilder();
            //Random random = new Random();
            char ch;
            for (int i = 0; i < 10; i++)
            {
                ch = Convert.ToChar(Convert.ToInt32(Math.Floor(26 * _random.NextDouble() + 65)));
                builder.Append(ch);
            }
            saveNewPassword(builder.ToString());
            return builder.ToString();
        }
        //zapis nowego hasła
        protected void saveNewPassword(string password)
        {
            string crytpPass = Crypt.GetMD5Hash(password);
            
            using (DentistEntities dB = new DentistEntities())
            {
                var query = dB.Patients.First(qr => qr.login == loginTextBox1.Text);
                query.password = crytpPass;
                try
                {
                    dB.SaveChanges();
                }
                catch (Exception ex)
                {
                    infoPlaceHolder.Visible = true;
                    infoLabel.Text = ex.Message;                    
                }
            }
        }
        //utworzenie wiadomości z hasłem
        protected string getMessage(string password)
        {
            string message = "Witamy!<br /> Poniżej zamieszczamy Pana/Pani nowe hasło. <br /> Nowe hasło: " + password + "<br /> Pozdrawiamy, <br /> Hoży doktorzy";
            return message;
        }

        
    }
}